<?php
require_once 'siteUtils.php';
require_once 'siteConfig.php';
session_start();

$userList			= Array();
$alphaUserList		= Array();
populateUserList();

function populateUserList() {
	global $userList;
	global $alphaUserList;
	global $DATA_DIR;
	global $METADATA_FILE;
	//Generate MetaData File.
	$metaDataFilePath	= $DATA_DIR.$METADATA_FILE;
	$shellCommand		= 'cd '.$DATA_DIR.'; grep HEADER * > '.$metaDataFilePath.';cd -';
	$fileSep			= ':';
	$marker				= '.sent';
	shell_exec($shellCommand);
	
	$lines				= file($metaDataFilePath);
	
	foreach ($lines as $line) {
		$splitLine	= split($fileSep, $line);
		$file		= trim($splitLine [0]);
		
		if ($file != '.' && $file != '..' && (strrpos($file, $marker) == (strlen($file) - strlen($marker))) && intval(trim($splitLine [2])) <=2500) {
			$userList [substr($file, 0, strlen($file) - strlen($marker))]		= trim($splitLine [2]);
			$alphaUserList [substr($file, 0, strlen($file) - strlen($marker))]	= trim($splitLine [2]);
		}
	}

	//Sort by most relevant user.
	arsort($userList, SORT_NUMERIC);

	//Sort alphabetically.
	ksort($alphaUserList);
}

function getCurrentUser() {
	global $userList;
	$userDD		= 'userSelectDD';
	if ($_REQUEST ['choiceMenu'] == 'ALPHA') {
		$userDD	= 'alphaUserSelectDD';
	}

	if ($_POST [$userDD] != '') {
		return $_POST [$userDD];
	}
	else {
		return $userList [0];
	}
}
?>

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="default.css" rel="stylesheet" type="text/css">
<title>Life Parser</title>
</head>

<body>
    <table align="center" id="lifeParserMain"  border="0" width="80%">
		<form name="userSelectForm" action="#" method=POST>
		<tr>
			<td class="siteTitle" colspan="2">Life Parser</td>
        </tr>
		<tr valign="top">
			<td>Choose a <strong>user</strong> from one of the lists.
			<input type="hidden" name="choiceMenu" value='MC'/>
			</td>
			<td>
				<span align="left">By mailbox size:</span>
				<select name="userSelectDD" onchange="choiceMenu.value='MC';userSelectForm.submit();" align="right">
					<?php
						$selected	= 'selected';
						foreach ($userList as $user => $mailCount) {
							if ($_POST ['userSelectDD'] =='') {
								$_POST ['userSelectDD']	= $user;
							}
							if ($_POST ['userSelectDD'] == $user) {
								echo '<option value="'.$user.'" '.$selected.'>'.$user.'</option>';
							}
							else {
								echo '<option value="'.$user.'">'.$user.'</option>';
							}
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>
			</td>
			<td>
				By alphabetical order:
				<select name="alphaUserSelectDD" onchange="choiceMenu.value='ALPHA';userSelectForm.submit();">
					<?php
						$selected	= 'selected';
						print_r($alphaUserList);
						foreach ($alphaUserList as $user => $mailCount) {
							if ($_POST ['alphaUserSelectDD'] == '') {
								$_POST ['alphaUserSelectDD']	= $user;
							}
							if ($_POST ['alphaUserSelectDD'] == $user) {
								echo '<option value="'.$user.'" '.$selected.'>'.$user.'</option>';
							}
							else {
								echo '<option value="'.$user.'">'.$user.'</option>';
							}
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>
				<?php
					echo 'Welcome <strong>'.getCurrentUser().'.</strong> You have <strong>'.$userList [getCurrentUser()].'</strong> mails.';		
				?>
			</td>
			<td>
			</td>
		</tr>
        <tr>
			<td colspan="2">
				<link rel="stylesheet" type="text/css" href="history/history.css" />
				<script type="text/javascript" src="history/history.js"></script>
				<script type="text/javascript" src="swfobject.js"></script>
				<script type="text/javascript">
					<!-- Taken from automatically generated FLEX HTML file -->
					<!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 

					var swfVersionStr = "10.0.0";

					<!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
					var xiSwfUrlStr = "playerProductInstall.swf";
					var userName	= escape("<?php echo getCurrentUser(); ?>");
					var flashvars = {"userId": userName };
					var params = {};

					<!-- Fill Params Here [Starts]-->
					params.quality = "high";
					params.bgcolor = "#ffffff";
					params.allowscriptaccess = "sameDomain";
					params.allowfullscreen = "true";

					<!-- Fill Params Here [Ends]-->
					var attributes = {};

					<!-- Fill Params Here [Starts]-->
					attributes.id = "LifeParserWeb";
					attributes.name = "LifeParserWeb";
					attributes.align = "middle";

					<!-- Fill Params Here [Ends]-->
					swfobject.embedSWF(
						"LifeParserWeb.swf", "flashContent", 
						"800", "600", 
						swfVersionStr, xiSwfUrlStr, 
						flashvars, params, attributes);

					<!-- JavaScript enabled so display the flashContent div in case it is not replaced with a swf object. -->

					swfobject.createCSS("#flashContent", "display:block;text-align:left;");
				</script>
				<div id="flashContent">
					<p>To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed. 
					</p>
					<script type="text/javascript"> 
						var pageHost = ((document.location.protocol == "https:") ? "https://" :	"http://"); 
						document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='"+ pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>" ); 
					</script>
				</div>
			</td>
        </tr>
		</form>
    </table>
</body>
</html>
